package com.learn.finished;

import java.util.HashSet;

/*
输入两个链表，找出它们的第一个公共节点。
如下面的两个链表：
https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/
*
* */
public class num_Offer_52 {
    /**
     * Definition for singly-linked list.
     */
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<Object> set = new HashSet<>();
        while(headA!=null){
            set.add(headA);
            headA=headA.next;
        }
        while (headB != null && !set.contains(headB)) {
            headB=headB.next;
        }
        return headB;

    }
}
